package com.sdcqjy.property.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.widget.ShowToast;
import com.sdcqjy.property.MyConstants;
import com.sdcqjy.property.R;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.presenter.RegPresenter;
import com.sdcqjy.property.presenter.contract.RegContract;
import com.sdcqjy.property.utils.SmsCodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/4/29.
 * 功能：
 */
public class RegActivity extends SimpleBaseActivity<RegPresenter> implements RegContract.View {
    @BindView(R.id.editName)
    EditText editName;
    @BindView(R.id.editTel)
    EditText editTel;
    @BindView(R.id.editCode)
    EditText editCode;
    @BindView(R.id.textCode)
    TextView textCode;
    @BindView(R.id.editPwd)
    EditText editPwd;
    @BindView(R.id.editPwd2)
    EditText editPwd2;
    @BindView(R.id.editEmail)
    EditText editEmail;
    @BindView(R.id.btnLogin)
    Button btnLogin;
    @BindView(R.id.imgCheck)
    ImageView imgCheck;
    private boolean isCheck;
    private SmsCodeUtils smsCodeUtils;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (MyConstants.ActionLogin.equals(action)) {
                if (intent.getBooleanExtra("login", false)) finish();
            }
        }
    };

    public static void open(Context context) {
        Intent intent = new Intent(context, RegActivity.class);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        ButterKnife.bind(this);
        presenter = new RegPresenter(this);

        smsCodeUtils = new SmsCodeUtils(textCode);

        IntentFilter filter = new IntentFilter(MyConstants.ActionLogin);
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
        isCheck = true;
    }

    protected void onResume() {
        super.onResume();
        smsCodeUtils.onResume();
    }

    protected void onDestroy() {
        super.onDestroy();
        smsCodeUtils.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
        receiver = null;
    }

    @OnClick(R.id.textCode)
    public void onTextCodeClicked() {
        String tel = editTel.getText().toString();
        if (TextUtils.isEmpty(tel)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsrsjh);
            return;
        }
        presenter.sendMsm(tel);
    }

    @OnClick(R.id.btnLogin)
    public void onBtnLoginClicked() {
        if (!isCheck) {
            ShowToast.alertShortOfWhite(getActivity(), "请同意用户协议");
            return;
        }
        String name = editName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsryhm);
            return;
        }
        String tel = editTel.getText().toString();
        if (TextUtils.isEmpty(tel)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsrsjh);
            return;
        }
        String code = editCode.getText().toString();
        if (TextUtils.isEmpty(code)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsryzm);
            return;
        }
        String pwd = editPwd.getText().toString();
        if (TextUtils.isEmpty(pwd)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsrdlmm);
            return;
        }
        String pwd2 = editPwd2.getText().toString();
        if (TextUtils.isEmpty(pwd2)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qqrdlmm);
            return;
        }
        if (!pwd.equals(pwd2)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.ncsrdmmbt);
            return;
        }
        String email = editEmail.getText().toString();
        if (TextUtils.isEmpty(email)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsryx);
            return;
        }
        presenter.reg(tel, name, pwd, code, email);
    }

    @OnClick({R.id.imgCheck, R.id.textCheck})
    public void onTextCheckClicked() {
        isCheck = !isCheck;
        imgCheck.setImageResource(isCheck ? R.drawable.radio_check_blue_ : R.drawable.radio_check_blue);
    }

    @OnClick(R.id.text)
    public void onTextClicked() {
        //presenter.注册协议();
    }

    public void sendMsmRet(BaseRet ret) {
        if (ret.isOk()) smsCodeUtils.start();
        else smsCodeUtils.stop();
    }

    public void regSucc() {
        finish();
    }
}
